package com.yndsoft.jc.supplier.perfect;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yndsoft.jc.R;
import com.yndsoft.jc.model.ReturnMsg;
import com.yndsoft.jc.supplier.adapter.CategoryAdapter1;
import com.yndsoft.jc.supplier.adapter.CategoryAdapter2;
import com.yndsoft.jc.supplier.adapter.ProductAdapter1;
import com.yndsoft.jc.supplier.adapter.ProductAdapter2;
import com.yndsoft.jc.supplier.base.SupplierBaseActivity;
import com.yndsoft.jc.supplier.model.CategoryModel;
import com.yndsoft.jc.supplier.model.EventModel;
import com.yndsoft.jc.supplier.model.ProductAttributeModel;
import com.yndsoft.jc.supplier.model.ProductModel;
import com.yndsoft.jc.supplier.modelput.Perfect;
import com.yndsoft.jc.supplier.net.util.ProgressSubscriber;
import com.yndsoft.jc.supplier.utils.Constants;
import com.yndsoft.jc.supplier.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

public class PerfectAddActivity extends SupplierBaseActivity implements View.OnClickListener {

    private TextView perfectAddType1;
    private RecyclerView perfectAddList1;
    private TextView perfectAddType2;
    private RecyclerView perfectAddList2;
    private TextView perfectAddProduct;
    private TextView perfectAddProduct1;
    private RecyclerView perfectAddList3;
    private TextView perfectAddProduct2;
    private RecyclerView perfectAddList4;
    private TextView perfectAddPrice;
    private EditText perfectAddMoney;
    private EditText perfectAddCycle;
    private Button perfectAddSave;

    private boolean toggle1 = false;
    private boolean toggle2 = false;
    private boolean toggle3 = false;
    private boolean toggle4 = false;
    private List<CategoryModel> typeModel;
    private List<ProductModel> productModel;
    private List<ProductAttributeModel> productAttributeModel;
    private String pId;
    private String pAId;

    @Override
    public int getContentResId() {
        return R.layout.activity_perfect_add;
    }

    public static void start(Context context) {
        context.startActivity(new Intent(context, PerfectAddActivity.class));
    }

    @Override
    public void initView() {
        ImageButton barBack = findViewById(R.id.bar_back);
        TextView barTitle = findViewById(R.id.bar_title);
        perfectAddType1 = findViewById(R.id.perfect_add_type_1);
        perfectAddList1 = findViewById(R.id.perfect_add_list_1);
        perfectAddType2 = findViewById(R.id.perfect_add_type_2);
        perfectAddList2 = findViewById(R.id.perfect_add_list_2);
        perfectAddProduct = findViewById(R.id.perfect_add_product);
        perfectAddProduct1 = findViewById(R.id.perfect_add_product_1);
        perfectAddList3 = findViewById(R.id.perfect_add_list_3);
        perfectAddProduct2 = findViewById(R.id.perfect_add_product_2);
        perfectAddList4 = findViewById(R.id.perfect_add_list_4);
        perfectAddPrice = findViewById(R.id.perfect_add_price);
        perfectAddMoney = findViewById(R.id.perfect_add_money);
        perfectAddCycle = findViewById(R.id.perfect_add_cycle);
        perfectAddSave = findViewById(R.id.perfect_add_save);

        barTitle.setText("完善项目");
        barBack.setOnClickListener(this);
        perfectAddType1.setOnClickListener(this);
        perfectAddType2.setOnClickListener(this);
        perfectAddProduct1.setOnClickListener(this);
        perfectAddProduct2.setOnClickListener(this);
        perfectAddSave.setOnClickListener(this);
    }

    @Override
    public void initData() {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bar_back:
                finish();
                break;
            case R.id.perfect_add_type_1:
                if (toggle1) {
                    setArrow(perfectAddType1, false);
                    perfectAddList1.setVisibility(View.GONE);
                    toggle1 = false;
                } else {
                    setArrow(perfectAddType1, true);
                    perfectAddList1.setVisibility(View.VISIBLE);
                    if (typeModel == null) {
                        mHttpUtil.category(new ProgressSubscriber(mContext) {
                            @Override
                            public void next(ReturnMsg returnMsg) {
                                String info = returnMsg.getInfo();
                                if (info != null) {
                                    typeModel = new Gson().fromJson(info, new TypeToken<List<CategoryModel>>() {
                                    }.getType());
                                    if (typeModel != null && typeModel.size() > 0) {
                                        perfectAddList1.setLayoutManager(new LinearLayoutManager(mContext));
                                        CategoryAdapter1 categoryAdapter1 = new CategoryAdapter1(mContext, typeModel);
                                        setListener1(categoryAdapter1);
                                        perfectAddList1.setAdapter(categoryAdapter1);
                                    }
                                }
                            }
                        }, "producttype");
                    }
                    toggle1 = true;
                }
                break;
            case R.id.perfect_add_type_2:
                if (toggle2) {
                    setArrow(perfectAddType2, false);
                    perfectAddList2.setVisibility(View.GONE);
                    toggle2 = false;
                } else {
                    setArrow(perfectAddType2, true);
                    perfectAddList2.setVisibility(View.VISIBLE);
                    toggle2 = true;
                }
                break;
            case R.id.perfect_add_product_1:
                if (toggle3) {
                    setArrow(perfectAddProduct1, false);
                    perfectAddList3.setVisibility(View.GONE);
                    toggle3 = false;
                } else {
                    setArrow(perfectAddProduct1, true);
                    perfectAddList3.setVisibility(View.VISIBLE);
                    toggle3 = true;
                }
                break;
            case R.id.perfect_add_product_2:
                if (toggle4) {
                    setArrow(perfectAddProduct2, false);
                    perfectAddList4.setVisibility(View.GONE);
                    toggle4 = false;
                } else {
                    setArrow(perfectAddProduct2, true);
                    perfectAddList4.setVisibility(View.VISIBLE);
                    toggle4 = true;
                }
                break;
            case R.id.perfect_add_save:
                String typeValue1 = perfectAddType1.getText().toString();
                String typeValue2 = perfectAddType2.getText().toString();
                String productValue1 = perfectAddProduct1.getText().toString();
                String productValue2 = perfectAddProduct2.getText().toString();
                String money = perfectAddMoney.getText().toString();
                String cycle = perfectAddCycle.getText().toString();
                if (TextUtils.isEmpty(typeValue1)) {
                    showHint("请选择父类别！");
                } else {
                    if (TextUtils.isEmpty(typeValue2)) {
                        showHint("请选择子类别！");
                    } else {
                        if (TextUtils.isEmpty(productValue1)) {
                            showHint("请选择父产品！");
                        } else {
                            if (TextUtils.isEmpty(productValue2)) {
                                showHint("请选择子产品！");
                            } else {
                                if (TextUtils.isEmpty(money)) {
                                    showHint("请填写价格！");
                                } else {
                                    if (TextUtils.isEmpty(cycle)) {
                                        showHint("请填写周期！");
                                    } else {
                                        Perfect perfect = new Perfect();
                                        perfect.setSid(mSession.getSid());
                                        perfect.setPid(pId);
                                        perfect.setPaid(pAId);
                                        perfect.setPrice(money);
                                        perfect.setTime(cycle);
                                        String json = new Gson().toJson(perfect);
                                        putSupplierProduct(json);
                                    }
                                }
                            }
                        }
                    }
                }
                break;
        }
    }

    private void putSupplierProduct(String json) {
        mHttpUtil.put(new ProgressSubscriber(mContext) {
            @Override
            public void next(ReturnMsg returnMsg) {
                if (1 == returnMsg.getCode()) {
                    EventModel model = new EventModel();
                    model.setInteger(Constants.PERFECT_ADD);
                    EventBus.getDefault().post(model);
                    ToastUtil.show(mContext, "保存成功!");
                    finish();
                } else if (2 == returnMsg.getCode()) {
                    ToastUtil.show(mContext, "该项目已存在!");
                } else {
                    ToastUtil.show(mContext, "保存失败!");
                }
            }
        }, "addproduct", json);
    }

    private void setArrow(TextView textView, boolean isOpen) {
        Drawable drawable = getResources().getDrawable(isOpen ? R.mipmap.select_open : R.mipmap.select_close);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        textView.setCompoundDrawables(null, null, drawable, null);
    }

    private void setListener1(CategoryAdapter1 categoryAdapter) {
        categoryAdapter.setOnChooseCategoryListener1(new OnChooseCategoryListener1() {
            @Override
            public void onChooseCategory1(CategoryModel model) {
                setArrow(perfectAddType1, false);
                perfectAddList1.setVisibility(View.GONE);
                perfectAddType2.setText("");
                perfectAddProduct1.setText("");
                perfectAddProduct2.setText("");
                productModel = null;
                productAttributeModel = null;
                toggle1 = false;
                if (model != null) {
                    perfectAddType1.setText(model.getName() != null ? model.getName() : "");
                    List<CategoryModel.DetsBean> dets = model.getDets();
                    if (dets != null && dets.size() > 0) {
                        perfectAddList2.setLayoutManager(new LinearLayoutManager(mContext));
                        CategoryAdapter2 categoryAdapter2 = new CategoryAdapter2(mContext, dets);
                        setListener2(categoryAdapter2);
                        perfectAddList2.setAdapter(categoryAdapter2);
                    }
                    perfectAddType2.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    private void setListener2(CategoryAdapter2 categoryAdapter) {
        categoryAdapter.setOnChooseCategoryListener2(new OnChooseCategoryListener2() {
            @Override
            public void onChooseCategory2(CategoryModel.DetsBean bean) {
                setArrow(perfectAddType2, false);
                perfectAddList2.setVisibility(View.GONE);
                perfectAddProduct1.setText("");
                perfectAddProduct2.setText("");
                productModel = null;
                productAttributeModel = null;
                toggle2 = false;
                if (bean != null) {
                    perfectAddType2.setText(bean.getName() != null ? bean.getName() : "");
                    loadProduct(bean.getId());
                    perfectAddProduct.setVisibility(View.VISIBLE);
                    perfectAddProduct1.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    private void loadProduct(String id) {
        if (productModel == null) {
            mHttpUtil.product(new ProgressSubscriber(mContext) {
                @Override
                public void next(ReturnMsg returnMsg) {
                    String info = returnMsg.getInfo();
                    if (info != null) {
                        productModel = new Gson().fromJson(info, new TypeToken<List<ProductModel>>() {
                        }.getType());
                        if (productModel != null && productModel.size() > 0) {
                            perfectAddList3.setLayoutManager(new LinearLayoutManager(mContext));
                            ProductAdapter1 productAdapter1 = new ProductAdapter1(mContext, productModel);
                            setListener3(productAdapter1);
                            perfectAddList3.setAdapter(productAdapter1);
                        }
                    }
                }
            }, "productlist", id, 0, 100);
        }
    }

    private void setListener3(ProductAdapter1 productAdapter1) {
        productAdapter1.setOnChooseProductListener1(new OnChooseProductListener1() {
            @Override
            public void onChooseProduct1(ProductModel model) {
                setArrow(perfectAddProduct1, false);
                perfectAddList3.setVisibility(View.GONE);
                perfectAddProduct2.setText("");
                productAttributeModel = null;
                toggle3 = false;
                if (model != null) {
                    perfectAddProduct1.setText(model.getName() != null ? model.getName() : "");
                    pId = model.getId();
                    loadProductAttribute(pId);
                    perfectAddProduct2.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    private void loadProductAttribute(String id) {
        if (productAttributeModel == null) {
            mHttpUtil.productAttribute(new ProgressSubscriber(mContext) {
                @Override
                public void next(ReturnMsg returnMsg) {
                    String info = returnMsg.getInfo();
                    if (info != null) {
                        productAttributeModel = new Gson().fromJson(info, new TypeToken<List<ProductAttributeModel>>() {
                        }.getType());
                        if (productAttributeModel != null && productAttributeModel.size() > 0) {
                            perfectAddList4.setLayoutManager(new LinearLayoutManager(mContext));
                            ProductAdapter2 productAdapter2 = new ProductAdapter2(mContext, productAttributeModel);
                            setListener4(productAdapter2);
                            perfectAddList4.setAdapter(productAdapter2);
                        }
                    }
                }
            }, "productattribute", id);
        }
    }

    private void setListener4(ProductAdapter2 productAdapter2) {
        productAdapter2.setOnChooseProductListener2(new OnChooseProductListener2() {
            @Override
            public void onChooseProduct2(ProductAttributeModel model) {
                setArrow(perfectAddProduct2, false);
                perfectAddList4.setVisibility(View.GONE);
                toggle4 = false;
                if (model != null) {
                    perfectAddProduct2.setText(model.getProject() + " " + model.getMethod() + " " + model.getStandard());
                    pAId = model.getId();
                    perfectAddPrice.setText(model.getPrice() != null ? "平台价格: ¥" + model.getPrice() : "平台价格: ¥");
                }
            }
        });
    }
}






































